﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace MikroMagazaObject
{
    public enum ReportTypes
    {
        AnlikSatisRaporu,
        GunlukSatisRaporu,
        AylikSatisRaporu,
        EncokSatan100Raporu,
        AnaGrupBazindaCiroRaporu,
        AltGrupBazindaCiroRaporu,
        ReyonBazindaCiroRaporu,
        UrunSatisRaporu
    }

    public partial class frmReports : XtraForm
    {
        public ReportTypes RaporTipi { get; set; }
        public string Path { get; set; }
        public string CnnString { get; set; }
        public string KdvSiralamasi { get; set; }


        public frmReports()
        {
            InitializeComponent();
        }

        private void frmReports_Load(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                switch (RaporTipi)
                {
                    case ReportTypes.AnlikSatisRaporu:
                        AnlikCiroRaporu();
                        break;
                    case ReportTypes.GunlukSatisRaporu:
                        GunlukCiroRaporu();
                        break;
                    case ReportTypes.AylikSatisRaporu:
                        AylikCiroRaporu();
                        break;
                    case ReportTypes.EncokSatan100Raporu:
                        EncokSatan100urunRaporu();
                        break;
                    case ReportTypes.AnaGrupBazindaCiroRaporu:
                        AnaGrupBazindaCiroRaporu();
                        break;
                    case ReportTypes.AltGrupBazindaCiroRaporu:
                        AltGrupBazindaCiroRaporu();
                        break;
                    case ReportTypes.ReyonBazindaCiroRaporu:
                        ReyonBazindaCiroRaporu();
                        break;
                    case ReportTypes.UrunSatisRaporu:
                        UrunSatisRaporu();
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        private void AnlikCiroRaporu()
        {
            CashProcess pr = new CashProcess();
            pr.CnnString = this.CnnString;
            pr.Path = this.Path;
            pr.KdvSiralamasi = this.KdvSiralamasi;
            List<Main> lines = pr.ImportData(new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day), new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59));
            var result = (from h in lines group h by new { h.KasaNo, h.EvrakTipi } into g select new { KasaNo = g.Key.KasaNo + ".Kasa", g.Key.EvrakTipi, Nakit = g.Sum(x => x.NakitToplam), Kredi = g.Sum(x => x.KrediTusToplam), Indirim = g.Sum(x => x.IndirimToplami), NetToplam = g.Sum(x => x.KdvDahilNetToplam), BurutToplam = g.Sum(x => x.KdvDahilBurutToplam) }).ToList();
            pivotGridControl1.DataSource = result;
            pivotGridControl1.Fields.Add("KasaNo", DevExpress.XtraPivotGrid.PivotArea.RowArea);
            pivotGridControl1.Fields.Add("EvrakTipi", DevExpress.XtraPivotGrid.PivotArea.RowArea);
            pivotGridControl1.Fields.Add("Nakit", DevExpress.XtraPivotGrid.PivotArea.DataArea);
            pivotGridControl1.Fields.Add("Kredi", DevExpress.XtraPivotGrid.PivotArea.DataArea);
            pivotGridControl1.Fields.Add("Indirim", DevExpress.XtraPivotGrid.PivotArea.DataArea);
            pivotGridControl1.Fields.Add("BurutToplam", DevExpress.XtraPivotGrid.PivotArea.DataArea);
            pivotGridControl1.Fields.Add("NetToplam", DevExpress.XtraPivotGrid.PivotArea.DataArea);
            this.Text = "Anlık Ciro Raporu";
        }

        private void GunlukCiroRaporu()
        {
            CashProcess pr = new CashProcess();
            pr.CnnString = this.CnnString;
            pr.Path = this.Path;
            pr.KdvSiralamasi = this.KdvSiralamasi;
            frmTarihFiltre fl = new frmTarihFiltre();
            fl.RaporTipi = ReportTypes.GunlukSatisRaporu;
            if (fl.ShowDialog() == DialogResult.OK)
            {
                this.Text = "Günlük Kıyaslamalı Ciro Raporu";
                List<Main> lines = pr.ImportData(fl.BaslangicTarihi, fl.BitisTarihi);
                var result = (from h in lines group h by new { h.KasaNo, h.Tarih } into g select new { KasaNo = g.Key.KasaNo + ".Kasa", g.Key.Tarih, Nakit = g.Sum(x => x.NakitToplam), Kredi = g.Sum(x => x.KrediTusToplam), Indirim = g.Sum(x => x.IndirimToplami), NetToplam = g.Sum(x => x.KdvDahilNetToplam), BurutToplam = g.Sum(x => x.KdvDahilBurutToplam) }).ToList();
                pivotGridControl1.DataSource = result;
                pivotGridControl1.Fields.Add("Tarih", DevExpress.XtraPivotGrid.PivotArea.RowArea);
                pivotGridControl1.Fields.Add("KasaNo", DevExpress.XtraPivotGrid.PivotArea.RowArea);
                pivotGridControl1.Fields.Add("Nakit", DevExpress.XtraPivotGrid.PivotArea.DataArea);
                pivotGridControl1.Fields.Add("Kredi", DevExpress.XtraPivotGrid.PivotArea.DataArea);
                pivotGridControl1.Fields.Add("Indirim", DevExpress.XtraPivotGrid.PivotArea.DataArea);
                pivotGridControl1.Fields.Add("BurutToplam", DevExpress.XtraPivotGrid.PivotArea.DataArea);
                pivotGridControl1.Fields.Add("NetToplam", DevExpress.XtraPivotGrid.PivotArea.DataArea);
            }
        }

        private void AylikCiroRaporu()
        {
            CashProcess pr = new CashProcess();
            pr.CnnString = this.CnnString;
            pr.Path = this.Path;
            pr.KdvSiralamasi = this.KdvSiralamasi;
            frmTarihFiltre fl = new frmTarihFiltre();
            fl.RaporTipi = ReportTypes.AylikSatisRaporu;
            if (fl.ShowDialog() == DialogResult.OK)
            {
                this.Text = "Aylık Kıyaslamalı Ciro Raporu";
                DateTime bitis = new DateTime(DateTime.Now.Year, fl.BitisAy, 1).AddMonths(1).AddDays(-1);

                List<Main> lines = pr.ImportData(new DateTime(DateTime.Now.Year, fl.BaslangicAy, 1), bitis);
                var result = (from h in lines group h by new { h.KasaNo, h.AyAdi } into g select new { KasaNo = g.Key.KasaNo + ".Kasa", Ay = g.Key.AyAdi, Nakit = g.Sum(x => x.NakitToplam), Kredi = g.Sum(x => x.KrediTusToplam), Indirim = g.Sum(x => x.IndirimToplami), NetToplam = g.Sum(x => x.KdvDahilNetToplam), BurutToplam = g.Sum(x => x.KdvDahilBurutToplam) }).ToList();
                pivotGridControl1.DataSource = result;
                pivotGridControl1.Fields.Add("Ay", DevExpress.XtraPivotGrid.PivotArea.RowArea);
                pivotGridControl1.Fields.Add("KasaNo", DevExpress.XtraPivotGrid.PivotArea.RowArea);
                pivotGridControl1.Fields.Add("Nakit", DevExpress.XtraPivotGrid.PivotArea.DataArea);
                pivotGridControl1.Fields.Add("Kredi", DevExpress.XtraPivotGrid.PivotArea.DataArea);
                pivotGridControl1.Fields.Add("Indirim", DevExpress.XtraPivotGrid.PivotArea.DataArea);
                pivotGridControl1.Fields.Add("BurutToplam", DevExpress.XtraPivotGrid.PivotArea.DataArea);
                pivotGridControl1.Fields.Add("NetToplam", DevExpress.XtraPivotGrid.PivotArea.DataArea);
            }
        }

        private void EncokSatan100urunRaporu()
        {
            CashProcess pr = new CashProcess();
            pr.CnnString = this.CnnString;
            pr.Path = this.Path;
            pr.KdvSiralamasi = this.KdvSiralamasi;
            frmTarihFiltre fl = new frmTarihFiltre();
            fl.RaporTipi = ReportTypes.GunlukSatisRaporu;
            if (fl.ShowDialog() == DialogResult.OK)
            {
                this.Text = "En çok satan 100 ürün";
                List<Main> lines = pr.ImportData(fl.BaslangicTarihi, fl.BitisTarihi);
                List<Child> childs = new List<Child>();
                foreach (Main m in lines)
                {
                    childs.AddRange(m.Childs.ToArray());
                }

                var stk = pr.Stocks();
                var result = (from h in (from s in childs join u in stk on s.Barkod equals u.Barkod select new { u.Barkod, u.Adi, s.Adet, s.KdvDahilBurutTutar, s.IndirimToplami }).AsEnumerable() group h by new { h.Barkod, h.Adi } into g select new { Barkodu = g.Key.Barkod, StokAdi = g.Key.Adi, Miktar = g.Sum(x => x.Adet), KdvDahilTutar = g.Sum(x => x.KdvDahilBurutTutar), IndirimTutari = g.Sum(x => x.IndirimToplami) }).OrderByDescending(x => x.Miktar).Take(100).ToList();

                pivotGridControl1.DataSource = result;
                pivotGridControl1.Fields.Add("Barkodu", DevExpress.XtraPivotGrid.PivotArea.RowArea);
                pivotGridControl1.Fields.Add("StokAdi", DevExpress.XtraPivotGrid.PivotArea.RowArea);
                pivotGridControl1.Fields.Add("Miktar", DevExpress.XtraPivotGrid.PivotArea.DataArea);
                pivotGridControl1.Fields.Add("KdvDahilTutar", DevExpress.XtraPivotGrid.PivotArea.DataArea);
                pivotGridControl1.Fields.Add("IndirimTutari", DevExpress.XtraPivotGrid.PivotArea.DataArea);
            }
        }


        private void UrunSatisRaporu()
        {
            CashProcess pr = new CashProcess();
            pr.CnnString = this.CnnString;
            pr.Path = this.Path;
            pr.KdvSiralamasi = this.KdvSiralamasi;
            frmTarihFiltre fl = new frmTarihFiltre();
            fl.RaporTipi = ReportTypes.UrunSatisRaporu;
            if (fl.ShowDialog() == DialogResult.OK)
            {
                this.Text = "Ürün Satış Raporu";
                List<Main> lines = pr.ImportData(fl.BaslangicTarihi, fl.BitisTarihi);
                List<Child> childs = new List<Child>();
                foreach (Main m in lines)
                {
                    childs.AddRange(m.Childs.ToArray());
                }

                var stk = pr.Stocks();
                var result = (from h in (from s in childs join u in stk on s.Barkod equals u.Barkod select new { u.Barkod, u.Adi, s.Adet, s.KdvDahilBurutTutar, s.IndirimToplami }).AsEnumerable() group h by new { h.Barkod, h.Adi } into g select new { Barkodu = g.Key.Barkod, StokAdi = g.Key.Adi, Miktar = g.Sum(x => x.Adet), KdvDahilTutar = g.Sum(x => x.KdvDahilBurutTutar), IndirimTutari = g.Sum(x => x.IndirimToplami) }).OrderByDescending(x => x.Miktar).Where(x => x.Barkodu == fl.Filtre || x.StokAdi.StartsWith(fl.Filtre)).ToList();

                pivotGridControl1.DataSource = result;
                pivotGridControl1.Fields.Add("Barkodu", DevExpress.XtraPivotGrid.PivotArea.RowArea);
                pivotGridControl1.Fields.Add("StokAdi", DevExpress.XtraPivotGrid.PivotArea.RowArea);
                pivotGridControl1.Fields.Add("Miktar", DevExpress.XtraPivotGrid.PivotArea.DataArea);
                pivotGridControl1.Fields.Add("KdvDahilTutar", DevExpress.XtraPivotGrid.PivotArea.DataArea);
                pivotGridControl1.Fields.Add("IndirimTutari", DevExpress.XtraPivotGrid.PivotArea.DataArea);
            }
        }

        private void AnaGrupBazindaCiroRaporu()
        {
            CashProcess pr = new CashProcess();
            pr.CnnString = this.CnnString;
            pr.Path = this.Path;
            pr.KdvSiralamasi = this.KdvSiralamasi;
            frmTarihFiltre fl = new frmTarihFiltre();
            fl.RaporTipi = ReportTypes.GunlukSatisRaporu;
            if (fl.ShowDialog() == DialogResult.OK)
            {
                this.Text = "Ana Grup Bazında Satış Raporu";
                List<Main> lines = pr.ImportData(fl.BaslangicTarihi, fl.BitisTarihi);
                List<Child> childs = new List<Child>();
                foreach (Main m in lines)
                {
                    childs.AddRange(m.Childs.ToArray());
                }

                var stk = pr.Stocks();
                var result = (from h in (from s in childs join u in stk on s.Barkod equals u.Barkod select new { u.AnaGrupAdi, s.Adet, s.KdvDahilBurutTutar, s.IndirimToplami }).AsEnumerable() group h by new { h.AnaGrupAdi } into g select new { g.Key.AnaGrupAdi, Miktar = g.Sum(x => x.Adet), KdvDahilTutar = g.Sum(x => x.KdvDahilBurutTutar), IndirimTutari = g.Sum(x => x.IndirimToplami), KdvDahilNetTutar = g.Sum(x => x.KdvDahilBurutTutar - x.IndirimToplami) }).OrderByDescending(x => x.Miktar).ToList();

                pivotGridControl1.DataSource = result;
                pivotGridControl1.Fields.Add("AnaGrupAdi", DevExpress.XtraPivotGrid.PivotArea.RowArea);
                pivotGridControl1.Fields[0].Width = 200;
                pivotGridControl1.Fields.Add("Miktar", DevExpress.XtraPivotGrid.PivotArea.DataArea);
                pivotGridControl1.Fields.Add("KdvDahilTutar", DevExpress.XtraPivotGrid.PivotArea.DataArea);
                pivotGridControl1.Fields.Add("IndirimTutari", DevExpress.XtraPivotGrid.PivotArea.DataArea);
                pivotGridControl1.Fields.Add("KdvDahilNetTutar", DevExpress.XtraPivotGrid.PivotArea.DataArea);

            }
        }

        private void AltGrupBazindaCiroRaporu()
        {
            CashProcess pr = new CashProcess();
            pr.CnnString = this.CnnString;
            pr.Path = this.Path;
            pr.KdvSiralamasi = this.KdvSiralamasi;
            frmTarihFiltre fl = new frmTarihFiltre();
            fl.RaporTipi = ReportTypes.GunlukSatisRaporu;
            if (fl.ShowDialog() == DialogResult.OK)
            {
                this.Text = "Alt Grup Bazında Satış Raporu";
                List<Main> lines = pr.ImportData(fl.BaslangicTarihi, fl.BitisTarihi);
                List<Child> childs = new List<Child>();
                foreach (Main m in lines)
                {
                    childs.AddRange(m.Childs.ToArray());
                }

                var stk = pr.Stocks();
                var result = (from h in (from s in childs join u in stk on s.Barkod equals u.Barkod select new { u.AnaGrupAdi, u.AltGrupAdi, s.Adet, s.KdvDahilBurutTutar, s.IndirimToplami }).AsEnumerable() group h by new { h.AnaGrupAdi, h.AltGrupAdi } into g select new { g.Key.AnaGrupAdi, g.Key.AltGrupAdi, Miktar = g.Sum(x => x.Adet), KdvDahilTutar = g.Sum(x => x.KdvDahilBurutTutar), IndirimTutari = g.Sum(x => x.IndirimToplami), KdvDahilNetTutar = g.Sum(x => x.KdvDahilBurutTutar - x.IndirimToplami) }).OrderByDescending(x => x.Miktar).ToList();

                pivotGridControl1.DataSource = result;
                pivotGridControl1.Fields.Add("AnaGrupAdi", DevExpress.XtraPivotGrid.PivotArea.RowArea);
                pivotGridControl1.Fields[0].Width = 200;
                pivotGridControl1.Fields.Add("AltGrupAdi", DevExpress.XtraPivotGrid.PivotArea.RowArea);
                pivotGridControl1.Fields[1].Width = 200;
                pivotGridControl1.Fields.Add("Miktar", DevExpress.XtraPivotGrid.PivotArea.DataArea);
                pivotGridControl1.Fields.Add("KdvDahilTutar", DevExpress.XtraPivotGrid.PivotArea.DataArea);
                pivotGridControl1.Fields.Add("IndirimTutari", DevExpress.XtraPivotGrid.PivotArea.DataArea);
                pivotGridControl1.Fields.Add("KdvDahilNetTutar", DevExpress.XtraPivotGrid.PivotArea.DataArea);

            }
        }

        private void ReyonBazindaCiroRaporu()
        {
            CashProcess pr = new CashProcess();
            pr.CnnString = this.CnnString;
            pr.Path = this.Path;
            pr.KdvSiralamasi = this.KdvSiralamasi;
            frmTarihFiltre fl = new frmTarihFiltre();
            fl.RaporTipi = ReportTypes.GunlukSatisRaporu;
            if (fl.ShowDialog() == DialogResult.OK)
            {
                this.Text = "Reyon Bazında Satış Raporu";
                List<Main> lines = pr.ImportData(fl.BaslangicTarihi, fl.BitisTarihi);
                List<Child> childs = new List<Child>();
                foreach (Main m in lines)
                {
                    childs.AddRange(m.Childs.ToArray());
                }

                var stk = pr.Stocks();
                var result = (from h in (from s in childs join u in stk on s.Barkod equals u.Barkod select new { u.ReyonAdi, s.Adet, s.KdvDahilBurutTutar, s.IndirimToplami }).AsEnumerable() group h by new { h.ReyonAdi } into g select new { g.Key.ReyonAdi, Miktar = g.Sum(x => x.Adet), KdvDahilTutar = g.Sum(x => x.KdvDahilBurutTutar), IndirimTutari = g.Sum(x => x.IndirimToplami), KdvDahilNetTutar = g.Sum(x => x.KdvDahilBurutTutar - x.IndirimToplami) }).OrderByDescending(x => x.Miktar).ToList();

                pivotGridControl1.DataSource = result;
                pivotGridControl1.Fields.Add("ReyonAdi", DevExpress.XtraPivotGrid.PivotArea.RowArea);
                pivotGridControl1.Fields[0].Width = 200;
                pivotGridControl1.Fields.Add("Miktar", DevExpress.XtraPivotGrid.PivotArea.DataArea);
                pivotGridControl1.Fields.Add("KdvDahilTutar", DevExpress.XtraPivotGrid.PivotArea.DataArea);
                pivotGridControl1.Fields.Add("IndirimTutari", DevExpress.XtraPivotGrid.PivotArea.DataArea);
                pivotGridControl1.Fields.Add("KdvDahilNetTutar", DevExpress.XtraPivotGrid.PivotArea.DataArea);

            }
        }

    }
}
